package com.example.checkctf.NetworkRequest;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Message;
import android.util.Base64;

import com.example.baselibrary.Bean.CheckResultBean;
import com.example.baselibrary.Bean.OneFaceCheckBean;
import com.example.baselibrary.Bean.PickFaceBean;
import com.example.baselibrary.Bean.PickResultBean;
import com.example.baselibrary.Utils.LogUtil;
import com.example.checkctf.Interface.NetResult;
import com.example.checkctf.ProcessorThink.ResultCode;
import com.google.gson.Gson;

public class ResultManger {

    private static ResultManger instance;
    CheckResultBean checkResultBean;
    PickResultBean pickResultBean;
    OneFaceCheckBean oneFaceCheckBean;
    private static NetResult netResult;

    public static synchronized ResultManger getInstance() {
        if (null == instance) {
            instance = new ResultManger();
        }
        return instance;
    }

    //根据类型 做事情  1.二维码
    public void optimizerResult(String ss) {

        try {
            checkResultBean = new Gson().fromJson(ss, CheckResultBean.class);
            LogUtil.v(checkResultBean.toString());
            int code = Integer.parseInt(checkResultBean.getCode());
//            code = 100009;
            switch (code) {
                case ResultCode.SUCCESS:
                    //正常请直接通过的
                    netResult.netResult(3, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.CHECKED:
                    //该票已检
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.SYSTEM_ERROR:
                    //系统异常
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.TO_COLLECT_INFORMATION:
                    //需要人脸验证的 也就是要人脸录入
                    netResult.netResult(4, checkResultBean);
                    netResult.faceAcceptResult(checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.TO_COMPARE:
                    //进行闸机比对
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.CHECK_TIME_BETWEEN_IS_NOT_ENOUGH:
                    //检票时间间隔未到
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.TIME_IS_INVAIBLE:
                    //不在运营时间内
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.TIME_IS_INVAIBLE2:
                    //不在检票策略时间内
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.UPLOAD_MESSAGE_FAIED:
                    //100007---上传采集信息失败
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.OUT_DATE:
                    //300010---该票已过期
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.QRCODE_IS_NOT_EXIST:
                    //300000---二维码编号不存在（无效票，该票已退订，该票不能过此闸机，无效票，证件信息不存在）
                    netResult.netResult(5, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.TODAY_COUNT_EXHAUST:
                    //100011---当天次数已用完
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                case ResultCode.Card_IS_NOT_EXIST:
                    //无效票，卡号不存在
                    netResult.netResult(2, checkResultBean);
                    LogUtil.v(checkResultBean.getMessage());
                    break;
                default:

                    break;
            }
        } catch (Exception e) {
            //返回结果有问题 联网失败
            LogUtil.v("解析出现问题" + ss);
        }

    }

    //处理 人脸采集结果
    public void PickFaceResult(String ss) {

        try {
            pickResultBean = new Gson().fromJson(ss, PickResultBean.class);
            int code = Integer.parseInt(pickResultBean.getCode());
            LogUtil.v("采集照片的回调： " + pickResultBean.toString());
            switch (code) {
                case 0:
                    //如果添加成功，那么重新检票一次   说明录入成功  修改界面  以及 重新 检票
                    netResult.faceSuccess(4, pickResultBean.getData().getRowKey());
                    break;
                default:
                    //添加失败 重新添加
                    netResult.faceDefut();
                    break;
            }

        } catch (Exception e) {
            LogUtil.v("采集照片的回调  解析出现问题");
        }

    }

    //处理 人脸对比  结果
    public void checkFaceResult(String ss) {
        try {
            oneFaceCheckBean = new Gson().fromJson(ss, OneFaceCheckBean.class);
            int code = Integer.parseInt(oneFaceCheckBean.getCode());
            OneFaceCheckBean.DataBean dataBean = oneFaceCheckBean.getData();
            if (code == 0) {
                if (dataBean != null) {
                    String rowKey = oneFaceCheckBean.getData().getRowKey();
                    //有rowKey 那么就去 检票
                    netResult.oneFaceCheck(4, rowKey);
                    LogUtil.v("1比N人脸对比成功，可通行1人");
                }
            } else {
                LogUtil.v("1比N人脸对比" + oneFaceCheckBean.getMessage());
            }
        } catch (Exception e) {
            LogUtil.v("1比N人脸对比 格式不一致");
        }
    }

    public static void setNetResultListener(NetResult netResultListener) {
        netResult = netResultListener;
    }
}
